﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Workflow2211A.Application.DTO.Inputs;
using Workflow2211A.Application.DTO;
using Workflow2211A.Application.Services;
using Microsoft.AspNetCore.Authorization;
using Workflow2211A.Application.DTO.Outputs;

namespace Workflow2211A.API.Controllers
{
    /// <summary>
    /// 流程服务
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]//授权过滤器
    public class WorkflowController : ControllerBase
    {
        //1.通过构造函数注入
        //2.可以通过服务提供商 IServiceProvider
        //3.通过[FromService]获取服务

        private readonly IWorkflowService _workflowService;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="workflowService"></param>
        public WorkflowController(IWorkflowService workflowService)
        {
            _workflowService = workflowService;
        }
        /// <summary>
        /// 添加流程定义
        /// </summary>
        /// <param name="input">输入参数</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public APIResult<int> AddDefinition(DefinitionAddInput input)
        {
            return _workflowService.AddDefinition(input);
        }
        /// <summary>
        /// 添加流程定义方法二
        /// </summary>
        /// <param name = "input" > 输入参数 </ param >
        /// < returns > 返回受影响行数 </ returns >
        //[HttpPost]
        //public APIResult<int> AddDefinition([FromServices] IWorkflowService workflowService,[FromBody] DefinitionAddInput input)
        //{
        //    return _workflowService.AddDefinition(input);
        //}

        /// <summary>
        /// 流程发起
        /// </summary>
        /// <param name="input">输入参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<string>> StartWorkflow(WorkflowStartInput input)
        {
            return await _workflowService.StartWorkflow(input);
        }

        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="input">输入参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> Audit(AuditInput input)
        { 
            return await _workflowService.Audit(input);
        }
        /// <summary>
        /// 获取代办
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public APIPageing<AuditOutput> GetTodoList(PageInput pageInput)
        {
            return  _workflowService.GetTodoList(pageInput);
        }
        /// <summary>
        /// 获取流程设计列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public APIPageing<WorkflowDefinitionOutput> GetDefinitions(PageInput input)
        {
            return _workflowService.GetDefinitions(input);
        }
        /// <summary>
        /// 查询流程实例
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public APIPageing<WorkflowInstanceOutput> GetWorkflows([FromQuery]WorkflowQueryInput input)
        { 
            return _workflowService.GetWorkflows(input);
        }
        /// <summary>
        /// 获取表单的详情
        /// </summary>
        /// <param name="workflowDefinitionId"></param>
        /// <returns></returns>
        [HttpGet]  //  /api/Workflow/GetFormTemplateDetail/1 只有一个参数时，且参数是ID类型时
        public APIResult<FormTemplateDetail> GetFormTemplateDetail(long workflowDefinitionId)
        { 
            return _workflowService.GetFormTemplateDetail(workflowDefinitionId);
        }
        /// <summary>
        /// 保存流程设计表单
        /// </summary>
        /// <param name="input">输入参数</param>
        /// <returns></returns>
        [HttpPost]
        public APIResult<int> SaveFormTemplate(SaveFormTemplateInput input)
        { 
            return _workflowService.SaveFormTemplate(input);
        }

        /// <summary>
        /// 获取流程详情
        /// </summary>
        /// <param name="workflowId">流程实例Id</param>
        /// <returns></returns>
        [HttpGet]
        public APIResult<WorkflowDetailOutput> GetWorkflowDetail(string workflowId)
        { 
           return _workflowService.GetWorkflowDetail(workflowId);   
        }
    }
}
